﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Net.NetworkInformation;
using System.Windows.Forms;

namespace FennecUI
{
    /// <summary>
    /// Interaction logic for Taskbar.xaml
    /// </summary>
    public partial class Taskbar : System.Windows.Controls.UserControl
    {
        public Taskbar()
        {
            InitializeComponent();

            DispatcherTimer redrawTimer = new DispatcherTimer();
            redrawTimer.Interval = TimeSpan.FromMilliseconds(500);
            redrawTimer.Tick += new EventHandler(redrawTimer_Tick);
            redrawTimer.Start();
            
            // get current network connection availability and listen for network change events
            network = NetworkInterface.GetIsNetworkAvailable();
            NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
        }

        void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            network = e.IsAvailable;
        }


        private bool network;

        void redrawTimer_Tick(object sender, EventArgs e)
        {
            Clock.Content = DateTime.Now.ToString(CLOCK_FORMAT);
            NetStatus.Source = network?netOn_bmp:netOff_bmp;

            PowerStatus powerMonitor = SystemInformation.PowerStatus;
            if (powerMonitor.BatteryLifePercent > 0.75)
                BatteryStatus.Source = batt1_bmp;
            else if (powerMonitor.BatteryLifePercent > 0.5)
                BatteryStatus.Source = batt2_bmp;
            else if (powerMonitor.BatteryLifePercent > 0.25)
                BatteryStatus.Source = batt3_bmp;
            else
                BatteryStatus.Source = batt4_bmp;
            ChargingStatus.Visibility = powerMonitor.PowerLineStatus == System.Windows.Forms.PowerLineStatus.Online ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
        }

        private const string CLOCK_FORMAT = "HH:mm  ddd dd MMM yyyy";

        private BitmapImage netOn_bmp = new BitmapImage(new Uri(@"/FennecUI;component/Assets/network_on.png", UriKind.Relative));
        private BitmapImage netOff_bmp = new BitmapImage(new Uri(@"/FennecUI;component/Assets/network_off.png", UriKind.Relative));
        private BitmapImage batt1_bmp = new BitmapImage(new Uri(@"/FennecUI;component/Assets/batt_1.png", UriKind.Relative));
        private BitmapImage batt2_bmp = new BitmapImage(new Uri(@"/FennecUI;component/Assets/batt_2.png", UriKind.Relative));
        private BitmapImage batt3_bmp = new BitmapImage(new Uri(@"/FennecUI;component/Assets/batt_3.png", UriKind.Relative));
        private BitmapImage batt4_bmp = new BitmapImage(new Uri(@"/FennecUI;component/Assets/batt_4.png", UriKind.Relative));
        
        private void Windows_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Form.ActiveForm.WindowState = FormWindowState.Minimized;
        }
    }
}
